<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0,minimum-scale=1.0,maximum-scale=1.0" />
    <meta name="author" content="火星科技 http://mars3d.cn " />
    <meta name="apple-touch-fullscreen" content="yes" />
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta name="apple-mobile-web-app-status-bar-style" content="black" />
    <meta name="format-detection" content="telephone=no" />
    <meta name="x5-fullscreen" content="true" />
    <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1" />
    <!-- 标题及搜索关键字 -->
    <meta name="keywords" content="火星科技,cesium,3D,GIS,marsgis,三维,地球,地图,开发,框架,系统,示例,资料,模型,离线,外包,合肥,安徽,中国" />
    <meta
      name="description"
      content="火星科技 合肥火星 合肥火星科技 合肥火星科技有限公司 leaflet leaflet框架 leaflet开发 cesium cesium开发 cesium框架 三维 地球 模型  gis marsgis 地图离线 地图开发 地图框架 地图外包 框架 开发 外包  地图离线 二维地图 三维地图 全景漫游 地理信息系统 云GIS 三维GIS GIS平台 WebGIS"
    />

    <link rel="shortcut icon" type="image/x-icon" href="http://mars3d.cn/favicon.ico" />
    <title>卫星成像范围预报 | Mars3D | 三维地图 | 火星科技 | 合肥火星科技有限公司</title>

    <!--第三方lib-->
    <script
      type="text/javascript"
      src="../lib/include-lib.js"
      libpath="../lib/"
      include="jquery,jquery.minicolors,jquery.range,layer,font-awesome,jedate,bootstrap,bootstrap-checkbox,admui-frame,haoutil,turf,mars3d,mars3d-space"
    ></script>

    <link href="css/style.css" rel="stylesheet" />
  </head>

  <body class="dark">
    <!--加载前进行操作提示，优化用户体验-->
    <div id="mask" class="signmask" onclick="removeMask()"></div>

    <div id="mars3dContainer" class="mars3d-container"></div>

    <!-- 面板 -->
    <div class="infoview">
      <form class="form-horizontal">
        <div class="form-group">
          <label for="txtAngle" class="col-xs-4 col-sm-4 control-label">卫星张角：</label>
          <div class="col-xs-8 col-sm-8" style="padding-left: 0px">
            <input id="txtAngle" type="range" min="1" max="70" step="1" value="10" />
          </div>
        </div>


        <div class="form-group">
          <label for="txtStartTime" class="col-xs-4 col-sm-4 control-label">开始时间：</label>
          <div class="col-xs-8 col-sm-8" style="padding-left: 0px">
            <input type="text" class="form-control" id="txtStartTime" placeholder="YYYY-MM-DD" />
          </div>
        </div>

        <div class="form-group">
          <label for="txtEndTime" class="col-xs-4 col-sm-4 control-label">结束时间：</label>
          <div class="col-xs-8 col-sm-8" style="padding-left: 0px">
            <input type="text" class="form-control" id="txtEndTime" placeholder="YYYY-MM-DD" />
          </div>
        </div>
        <div class="form-group" title="地面成像范围区域的颜色">
          <label for="txtColor" class="col-xs-4 col-sm-4 control-label">区域颜色：</label>
          <div class="col-xs-8 col-sm-8" style="padding-left: 0px">
            <input type="text" id="txtColor" class="form-control" style="width: 100%" value="#ff0000" />
          </div>
        </div>

        <div class="form-group">
          <label for="txtOpacity" class="col-xs-4 col-sm-4 control-label">区域透明度</label>
          <div class="col-xs-8 col-sm-8" style="padding-left: 0px">
            <input id="txtOpacity" type="range" min="0.01" max="1.00" step="0.01" value="0.40" />
          </div>
        </div>
        <div class="form-group" style="text-align: center">
          <input id="btnAdd" type="button" class="btn btn-primary" value="计算成像区域" />
          <input id="btnRemoveAll" type="button" class="btn btn-danger" value="清除所有" />
        </div>


        <div class="form-group">
          <div class="col-xs-offset-3 col-sm-offset-3 col-xs-9 col-sm-9">
            <div class="checkbox checkbox-primary checkbox-inline">
              <input id="chkAngleS" class="styled" type="checkbox" checked />
              <label for="chkAngleS"> 升轨 </label>
            </div>
            <div class="checkbox checkbox-primary checkbox-inline">
              <input id="chkAngleJ" class="styled" type="checkbox" checked />
              <label for="chkAngleJ"> 降轨 </label>
            </div>
          </div>
        </div>

      </form>
    </div>

    <script src="./js/common.js"></script>
    <script type="text/javascript">
      'use script' //开发环境建议开启严格模式

      var map
      var graphicLayer

      function initMap(options) {
        //合并属性参数，可覆盖config.json中的对应配置
        var mapOptions = mars3d.Util.merge(options, {
          scene: {
            //此处参数会覆盖config.json中的对应配置
            center: {
              y: 10,
              x: 111.833884,
              z: 15000000,
              heading: 0,
              pitch: -90,
              roll: 0,
            },
            cameraController: {
              zoomFactor: 3.0,
              minimumZoomDistance: 1000,
              maximumZoomDistance: 300000000,
              constrainedAxis: false, //解除在南北极区域鼠标操作限制
            },
          },
          control: {
            animation: true, //是否创建动画小器件，左下角仪表
            timeline: true, //是否显示时间线控件
          },
        })

        //创建三维地球场景
        map = new mars3d.Map('mars3dContainer', mapOptions)

        //创建矢量数据图层
        graphicLayer = new mars3d.layer.GraphicLayer()
        map.addLayer(graphicLayer)

        graphicLayer.bindPopup(function (event) {
          var points = event.graphic?.attr?.points
          if (!points) {
            return
          }
          //单击轨迹连线上的点后，求该点对应的时间
          var positionDM = event.cartesian
          var val1 = points[0]
          var val2 = points[points.length - 1]

          var startTime = Cesium.JulianDate.toDate(val1.time)
          var endTime = Cesium.JulianDate.toDate(val2.time)

          var len1 = Math.abs(Cesium.Cartesian3.distance(positionDM, val1.position))
          var len2 = Math.abs(Cesium.Cartesian3.distance(positionDM, val2.position))

          var adds = (len1 / (len1 + len2)) * (endTime.getTime() - startTime.getTime()) //求按距离比例的时间增加值
          var currentTime = new Date(startTime.getTime() + adds)

          var inthtml = '单击处时间：' + currentTime.format('yyyy-MM-dd HH:mm:ss')
          return inthtml
        })
        var weixin = new mars3d.graphic.Satellite({
          name: 'GAOFEN 1',
          tle1: '1 39150U 13018A   21180.50843864  .00000088  00000-0  19781-4 0  9997',
          tle2: '2 39150  97.8300 252.9072 0018449 344.7422  15.3253 14.76581022440650',
          model: {
            url: '//data.mars3d.cn/gltf/mars/weixin.gltf',
            scale: 1,
            minimumPixelSize: 90,
            autoHeading: true,
            show: true,
          },
          label: {
            color: '#ffffff',
            opacity: 1,
            font_family: '楷体',
            font_size: 30,
            outline: true,
            outlineColor: '#000000',
            outlineWidth: 3,
            background: true,
            backgroundColor: '#000000',
            backgroundOpacity: 0.5,
            font_weight: 'normal',
            font_style: 'normal',
            pixelOffsetX: 0,
            pixelOffsetY: -20,
            scaleByDistance: true,
            scaleByDistance_far: 10000000,
            scaleByDistance_farValue: 0.4,
            scaleByDistance_near: 100000,
            scaleByDistance_nearValue: 1,
            show: true,
          },
          cone: {
            sensorType: mars3d.graphic.SatelliteSensor.Type.Rect,
            angle1: 10,
            angle2: 5,
            color: '#6ef500',
            show: true,
          },
          path: {
            show: true,
            color: '#00ff00',
            opacity: 0.5,
            width: 1,
          },
        })
        map.graphicLayer.addGraphic(weixin)

        setTimeout(() => {
          weixin.flyTo({
            radius: 900000, //距离目标点的距离
            pitch: -60, //相机方向
          })
        }, 1000)

        //ui相关
        jeDate('#txtStartTime', {
          theme: {
            bgcolor: '#135b91',
            pnColor: '#00CCFF',
          },
          format: 'YYYY-MM-DD hh:mm:ss',
        })
        jeDate('#txtEndTime', {
          theme: {
            bgcolor: '#135b91',
            pnColor: '#00CCFF',
          },
          format: 'YYYY-MM-DD hh:mm:ss',
        })

        var now = Cesium.JulianDate.toDate(map.clock.currentTime)
        $('#txtStartTime').val(now.format('yyyy-MM-dd HH:mm:ss'))

        now.setMinutes(now.getMinutes() + 60)
        $('#txtEndTime').val(now.format('yyyy-MM-dd HH:mm:ss'))

        $('#txtColor').minicolors({
          position: 'bottom left',
          control: 'saturation',
          change: function (hex) {},
        })
        map.on(mars3d.EventType.click, function (e) {
          $('#txtColor').minicolors('hide')
        })

        $('#txtOpacity').range({
          onChange: function (value, bfb) {},
        })

        $('#txtAngle').range({
          onChange: function (value, bfb) {
            weixin.angle1 = value
          },
        })

        $('#chkAngleS').change(function () {
          var val = $(this).is(':checked')
          updateVisibleForFaceNouth(true, val)
        })

        $('#chkAngleJ').change(function () {
          var val = $(this).is(':checked')
          updateVisibleForFaceNouth(false, val)
        })

        $('#btnAdd').click(function () {
          var _starttime = $.trim($('#txtStartTime').val())
          var _endTime = $.trim($('#txtEndTime').val())
          var _color = $('#txtColor').val()
          var _opacity = Number($('#txtOpacity').val())
          var _angle = Number($('#txtAngle').val())

          addTimeShading(weixin, {
            startTime: _starttime,
            endTime: _endTime,
            color: _color,
            opacity: _opacity,
            angle: _angle,
          })
        })

        $('#btnRemoveAll').click(function () {
          graphicLayer.clear()
        })
      }

      //options参数包含：startTime开始时间，endTime结束时间，angle张开角度，color颜色，opacity透明度
      function addTimeShading(weixin, options) {
        graphicLayer.clear()

        let bt = new Date(options.startTime).getTime()
        let et = new Date(options.endTime).getTime()

        if (bt >= et) {
          haoutil.msg('开始时间需要小于结束时间。')
          return
        }

        var step = 2 * 60000

        var points = []
        var positions = []

        var temp_t = bt
        while (temp_t <= et) {
          var point = weixin.tle.getPoint(temp_t)
          if (!point) {
            break
          }

          var ground_pos = Cesium.Cartesian3.fromDegrees(point.lng, point.lat)
          positions.push(ground_pos)

          var time = Cesium.JulianDate.fromDate(new Date(temp_t))
          points.push({
            position: ground_pos,
            time: time,
            height: point.alt,
          })

          temp_t += step
        }

        if (positions.length < 2) {
          haoutil.msg('没有该时间段内的轨迹数据。')
          return
        }

        var clr = Cesium.Color.fromCssColorString(options.color || '#ff0000').withAlpha(Number(options.opacity || 1.0))
        clr = Cesium.ColorGeometryInstanceAttribute.fromColor(clr)

        var geometryInstancesSG = [] //升轨
        var geometryInstancesJG = [] //降轨
        for (var i = 1; i < positions.length; i++) {
          var position1 = positions[i - 1]
          var position2 = positions[i]

          var height = points[i].height //也可以取position2的高度
          var shadingWidth = height * Math.tan(Cesium.Math.toRadians(options.angle)) * 2 //根据卫星角度求其带宽度

          var instance = new Cesium.GeometryInstance({
            geometry: new Cesium.CorridorGeometry({
              vertexFormat: Cesium.VertexFormat.POSITION_ONLY,
              positions: [position1, position2],
              width: shadingWidth,
              cornerType: Cesium.CornerType.MITERED, //指定转角处样式
            }),
            attributes: {
              color: clr,
            },
          })
          //重要：绑定相关属性
          instance.attr = {
            points: [points[i - 1], points[i]],
          }

          var hp = mars3d.PointUtil.getHeadingPitchRollForLine(position1, position2)

          if (hp.heading > 0) {
            geometryInstancesSG.push(instance)
          } else {
            geometryInstancesJG.push(instance)
          }
        }

        //升轨
        if (geometryInstancesSG.length > 0) {
          var primitiveSG = new mars3d.graphic.BaseCombine({
            instances: geometryInstancesSG,
          })
          primitiveSG.isFaceNouth = true
          graphicLayer.addGraphic(primitiveSG)
        }

        //降轨
        if (geometryInstancesJG.length > 0) {
          var primitiveJG = new mars3d.graphic.BaseCombine({
            instances: geometryInstancesJG,
          })
          primitiveJG.isFaceNouth = false
          graphicLayer.addGraphic(primitiveJG)
        }
      }

      //升降轨类型筛选
      function updateVisibleForFaceNouth(isFaceNouth, show) {
        graphicLayer.eachGraphic(function (graphic) {
          if (isFaceNouth && graphic.isFaceNouth) {
            graphic.show = show
          }
          if (!isFaceNouth && !graphic.isFaceNouth) {
            graphic.show = show
          }
        })
      }
    </script>
  </body>
</html>
